﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace BOL.Shopping
{
    public class ShoppingCart
    {
        // khoi tao chinh doi tuong Shopping
        public static ShoppingCart Instance = new ShoppingCart();
        public DataTable dataItems = new DataTable();
        public Customer customer = new Customer();
        public string payment;
        public string datetime;
        public string orderid;

        public List<Item> mycart
        {
            get
            {
                // tao mot doi tuong list< CartIterm> 
                // gan session hien tai shopping bang list<CartIterm> 
                List<Item> sh = HttpContext.Current.Session["SHOPPING"] as List<Item>;
                if (HttpContext.Current.Session["SHOPPING"] == null)
                    sh = new List<Item>();
                HttpContext.Current.Session["SHOPPING"] = sh;
                return sh;
            }
        }

        // them vao gio hang 
       
        public void AddItem(Item item)
        {
            // cho san pham chua co trong gio hang
            Boolean has = false;
            // kiem tra xem trong gio hang da co san pham chua
            for (int i = 0; i < this.mycart.Count; i++)
            {
                if (item.id==mycart[i].id)
                {
                    has = true;// san pham da co trong gio hang
                    break;
                }
            }
            // neu san pham chua co trong gio hang thi them vao gio hang
            if (has == false)
            {
                this.mycart.Add(item);
            }
        }
        public void AddItem(string id)
        {
            // cho san pham chua co trong gio hang
            Boolean has = false;
            // kiem tra xem trong gio hang da co san pham chua

            for (int i = 0; i < this.mycart.Count; i++)
            {
                if (id == mycart[i].id)
                {
                    has = true;// san pham da co trong gio hang
                    break;
                }
            }
            // neu san pham chua co trong gio hang thi them vao gio hang
            if (has == false)
            {
                BOL.Shopping.Item item = new Item(id);
                this.mycart.Add(item);
            }
        }
        // xoa banh trong gio hang
        public void RemoveItem(Item item)
        {
            
            // duyet va kiem tra xem co trong gio ko 
            for (int i = 0; i < this.mycart.Count; i++)
            {
                if (item.id == mycart[i].id)
                {
                     this.mycart.Remove(this.mycart[i]);
                    
                    break;
                }
            }
           
        }
        public void RemoveItem(string id)
        {

            // duyet va kiem tra xem co trong gio ko 
            for (int i = 0; i < this.mycart.Count; i++)
            {
                if (id == mycart[i].id)
                {
                    this.mycart.Remove(this.mycart[i]);

                    break;
                }
            }

        }
        public string getIDProduct()
        {
            string s = "";
            int i = 0;
            if (mycart.Count > 0)
            {
                for (i = 0; i < this.mycart.Count - 1; i++)
                {
                    s += mycart[i].id.Split('-')[0].ToString() + ",";
                }
                s += mycart[i].id.Split('-')[0].ToString();
            }
            else
            {
                return "-1";
            }
            return s;

        }

        public string getIDColor()
        {
            string s = "";
            int i = 0;
            if (mycart.Count > 0)
            {
                for (i = 0; i < this.mycart.Count - 1; i++)
                {
                    s += mycart[i].id.Split('-')[1].ToString() + ",";
                }
                s += mycart[i].id.Split('-')[1].ToString();
            }
            else
            {
                return "-1";
            }
            return s;
        }
        public string getIDSize()
        {
            string s = "";
            int i = 0;
            if (mycart.Count > 0)
            {
                for (i = 0; i < this.mycart.Count - 1; i++)
                {
                    s += mycart[i].id.Split('-')[2].ToString() + ",";
                }
                s += mycart[i].id.Split('-')[2].ToString();
            }
            else
            {
                return "-1";
            }
            return s;
        }
        public int getSoLuong(string itemID)
        {
            for (int i = 0; i < this.mycart.Count; i++)
            {
                if (itemID == mycart[i].id)
                {
                    return mycart[i].soluong;
                }
            }

            return 0;
        }
        public void  UpdateDataItems()
        {

            string sql = "select *, Product_SalesPrice - round(((Product_SalesPrice * Product_discount)/100),0) as newprice  from tbl_Product,tbl_ProductColor,tbl_ProductSize , tbl_Size, tbl_Color where tbl_Product.Product_ID = tbl_ProductColor.Product_ID AND tbl_Color.Color_ID = tbl_ProductColor.Color_ID AND tbl_Size.Size_ID = tbl_ProductSize.Size_ID  AND tbl_ProductColor.ProductColor_ID = tbl_ProductSize.ProductColor_ID AND tbl_Product.Product_ID IN ( ";

            sql += getIDProduct();
            sql += ") AND tbl_ProductColor.ProductColor_ID IN (";
            sql += getIDColor();
            sql += ") AND tbl_ProductSize.ProductSize_ID IN ( " + getIDSize() + ")";



            dataItems = DAL.DCollector.DBCollector.Instance.CallStored("callstored",sql,true);
        }

        public DataTable getDataItems()
        {
       
            return dataItems;
        }
        public bool checkID(string id)
        {
            // cho san pham chua co trong gio hang
            Boolean has = false;
            // kiem tra xem trong gio hang da co san pham chua
            for (int i = 0; i < this.mycart.Count; i++)
            {
                if (id == mycart[i].id)
                {
                    has = true;// san pham da co trong gio hang
                    break;
                }
            }
            // neu san pham chua co trong gio hang thi them vao gio hang
            return has;
        }
        public void setQuantity(string id, int sl)
        {
            for (int i = 0; i < this.mycart.Count; i++)
            {
                if (id == mycart[i].id)
                {
                    mycart[i].soluong = sl;
                    break;
                }
            }
        }
        public double getSum()
        {
            double sum = 0;
            for (int i = 0; i < this.mycart.Count; i++)
            {
                sum += getPrice(mycart[i].id) * mycart[i].soluong;
          
            }
            return sum;

        }
        public string getColorName(string id)
        {
            for (int i = 0; i < this.dataItems.Rows.Count; i++)
            {
                if (id.Split('-')[0].ToString() == this.dataItems.Rows[i]["Product_ID"].ToString() && id.Split('-')[1].ToString() == this.dataItems.Rows[i]["ProductColor_ID"].ToString() && id.Split('-')[2].ToString() == this.dataItems.Rows[i]["ProductSize_ID"].ToString())
                    return dataItems.Rows[i]["Color_Name"].ToString();
            }
            return null;
        }
          public string getSizeName(string id)
        {
            for (int i = 0; i < this.dataItems.Rows.Count; i++)
            {
                if (id.Split('-')[0].ToString() == this.dataItems.Rows[i]["Product_ID"].ToString() && id.Split('-')[1].ToString() == this.dataItems.Rows[i]["ProductColor_ID"].ToString() && id.Split('-')[2].ToString() == this.dataItems.Rows[i]["ProductSize_ID"].ToString() )
                    return dataItems.Rows[i]["Size_Name"].ToString();
            }
            return null;
        }
        public double getPrice(string id)
        {
            for (int i = 0; i < this.dataItems.Rows.Count; i++)
            {
                if (id.Split('-')[0].ToString() == this.dataItems.Rows[i]["Product_ID"].ToString() && id.Split('-')[1].ToString() == this.dataItems.Rows[i]["ProductColor_ID"].ToString() && id.Split('-')[2].ToString() == this.dataItems.Rows[i]["ProductSize_ID"].ToString())
                    return Convert.ToDouble(dataItems.Rows[i]["newprice"].ToString());
            }
            return 0;

        }
        public string getIDProduct(string id)
        {
            return id.Split('-')[0].ToString();
        }
        public string getIDColor(string id)
        {
            return id.Split('-')[1].ToString();
        }
        public string getIDSize(string id)
        {
            return id.Split('-')[2].ToString();
        }
        public string getName(string  id)
        {
            for (int i = 0; i < this.dataItems.Rows.Count; i++)
            {
                if (getIDProduct(id) == this.dataItems.Rows[i]["Product_ID"].ToString() )
                    return (dataItems.Rows[i]["Product_Name"].ToString());
            }
            return null;

        }
        public double getTotal(string  id)
        {
            for (int i = 0; i < this.dataItems.Rows.Count; i++)
            {
                if (id.Split('-')[0].ToString() == this.dataItems.Rows[i]["Product_ID"].ToString() && id.Split('-')[1].ToString() == this.dataItems.Rows[i]["ProductColor_ID"].ToString() && id.Split('-')[2].ToString() == this.dataItems.Rows[i]["ProductSize_ID"].ToString())
                    return Convert.ToDouble(dataItems.Rows[i]["newprice"].ToString())*getQuantity(id);
            }
            return 0;

        }
        public int getQuantity(string id)
        {

            for (int i = 0; i < this.mycart.Count; i++)
            {
                if (id == mycart[i].id)
                {
                    return mycart[i].soluong;
                    
                }
            }
            return 1;
        }
        public int getQuantityProduct()
        {

            if (dataItems == null) return 0;
            else
                return dataItems.Rows.Count;
        }
    }
}
